import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/basic/user_info.dart';
import 'package:lcapp/config/const_config.dart';
import 'package:lcapp/page/min/min_collection_page.dart';
import 'package:lcapp/page/min/min_video_page.dart';
import 'package:lcapp/page/min/min_xianhuo_page.dart';
import 'package:lcapp/widget/asr_cache_image.dart';

class MinPage extends StatefulWidget {
  const MinPage({Key? key}) : super(key: key);

  @override
  _MinPageState createState() => _MinPageState();
}

class _MinPageState extends State<MinPage> with SingleTickerProviderStateMixin{

  late TabController _tabController;
  ScrollController _scrollController = ScrollController();
  GlobalKey _tabKey = GlobalKey();
  double tabHeight = 30.w;
  @override
  void initState() {
    // TODO: implement initState
    _tabController = TabController(length: 4, vsync: this);
    refreshUserInfo(refreshBlock: (){setState(() {});});
    _scrollController.addListener(_scListiener);
  }

  void _scListiener(){
    final RenderBox renderBox = _tabKey.currentContext?.findRenderObject()! as RenderBox;
    Offset offset = renderBox.localToGlobal(Offset.zero);
    print('offset========' + (offset.dy + renderBox.size.height).toString());
    print('status========' + ScreenUtil().statusBarHeight.toString());
    setState(() {
      if(offset.dy + renderBox.size.height <= 0.w){
        tabHeight = ScreenUtil().statusBarHeight;
      }else{
        tabHeight = 30.w;
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _scrollController.removeListener(_scListiener);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: bgColor,
      body: NestedScrollView(
        controller: _scrollController,
        headerSliverBuilder: (context,vale){
          return [
            SliverToBoxAdapter(
              child: Opacity(
                opacity: 1,
                child: Container(
                  width: 1.sw,
                  key: _tabKey,
                  child: Stack(
                    children: [
                      Container(
                        child: Image.asset('assets/min/min_top_bg.png',width: 1.sw,fit: BoxFit.fitWidth,),
                      ),
                      Container(
                        // color: Colors.green,
                        margin: EdgeInsets.only(top: 150.w),
                        height: 150.w,
                        padding: EdgeInsets.symmetric(horizontal: 50.w),
                        child: Row(
                          children: [
                            Container(
                              height: 150.w,
                              width: 150.w,
                              alignment: Alignment.center,
                              decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(150.w,)
                              ),
                              child: Container(
                                height: 140.w,
                                width: 140.w,
                                child: ClipRRect(
                                  borderRadius: BorderRadius.circular(140.w,),
                                  child: ASRCacheImage(
                                    image: userInfo.img??'assets/icon/p_head.png',
                                    fit: BoxFit.fitWidth,
                                    width: 140.w,
                                    height: 140.w,
                                  ),
                                ),
                              ),
                            ),
                            GestureDetector(
                              onTap: (){
                                Navigator.of(context).pushNamed('FollowUsersPage',arguments: 0).then((value){
                                  refreshUserInfo(refreshBlock: (){
                                    setState(() {

                                    });
                                  });
                                });
                              },
                              child: Container(
                                margin: EdgeInsets.only(left: 80.w),
                                color: Colors.transparent,
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text('${userInfo.like}',style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 60.sp,
                                        fontWeight: FontWeight.w600
                                    ),),
                                    SizedBox(height:20.w),
                                    Text('收到意向',style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 30.sp,
                                    ),)
                                  ],
                                ),
                              ),
                            ),
                            GestureDetector(
                              onTap: (){
                                Navigator.of(context).pushNamed('FollowUsersPage',arguments: 1).then((value){
                                  refreshUserInfo(refreshBlock: (){
                                    setState(() {

                                    });
                                  });
                                });
                              },
                              child: Container(
                                color: Colors.transparent,
                                margin: EdgeInsets.only(left: 80.w),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text('${userInfo.fans}',style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 60.sp,
                                        fontWeight: FontWeight.w600
                                    ),),
                                    SizedBox(height:20.w),
                                    Text('粉丝',style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 30.sp,
                                    ),)
                                  ],
                                ),
                              ),
                            ),
                            GestureDetector(
                              onTap: (){
                                Navigator.of(context).pushNamed('FollowUsersPage',arguments: 2).then((value){
                                  refreshUserInfo(refreshBlock: (){
                                    setState(() {

                                    });
                                  });
                                });
                              },
                              child: Container(
                                color: Colors.transparent,
                                margin: EdgeInsets.only(left: 80.w),
                                child: Column(
                                  mainAxisAlignment: MainAxisAlignment.center,
                                  children: [
                                    Text('${userInfo.attention}',style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 60.sp,
                                        fontWeight: FontWeight.w600
                                    ),),
                                    SizedBox(height:20.w),
                                    Text('关注',style: TextStyle(
                                      color: Colors.white,
                                      fontSize: 30.sp,
                                    ),)
                                  ],
                                ),
                              ),
                            ),
                          ],
                        ),
                      ),
                      Container(
                        margin: EdgeInsets.only(left: 30.w,right: 30.w,top: 330.w,),
                        decoration: BoxDecoration(
                            color: Colors.white,
                            borderRadius: BorderRadius.circular(10.w)
                        ),
                        padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: [
                            Container(
                              child: Row(
                                children: [
                                  Expanded(child: Container(child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          children: [
                                            Text(userInfo.name??'',style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 40.sp,
                                                fontWeight: FontWeight.w600
                                            ),),
                                            SizedBox(width: 10.w,),
                                            Visibility(child: Image.asset('assets/min/min_sm.png',height: 30.w,),visible: userInfo.card==1,),
                                            SizedBox(width: 10.w,),
                                            Visibility(child: Image.asset('assets/min/min_vip.png',height: 30.w,),visible: userInfo.vip==1,),
                                          ],
                                        ),
                                      ),
                                      SizedBox(height: 10.w,),
                                      Visibility(child: Container(
                                        child: Row(
                                          children: [
                                            Image.asset('assets/home/smrz_succ.png',height: 26.w,),
                                            SizedBox(width: 10.w,),
                                            Text(userInfo.realName??'',style: TextStyle(
                                                color: Colors.black,
                                                fontSize: 24.sp,
                                                fontWeight: FontWeight.w400
                                            ),),

                                          ],
                                        ),
                                      ),visible: userInfo.card==1,),
                                      SizedBox(height: 10.w,),
                                      Visibility(child: Container(
                                        child: Row(
                                          children: [
                                            Image.asset('assets/min/min_company.png',height: 30.w,),
                                            SizedBox(width: 10.w,),
                                            Text(userInfo.company??'',style: TextStyle(
                                              color: Color(0xff5A5A5A),
                                              fontSize: 26.sp,
                                            ),),
                                          ],
                                        ),
                                      ),visible: userInfo.company!=null&&userInfo.company!.isNotEmpty,)
                                    ],
                                  ),)),
                                  GestureDetector(
                                    onTap: (){
                                      Navigator.of(context).pushNamed('MinEWMPage',arguments: userInfo);
                                    },
                                    child: Container(
                                      child: Image.asset('assets/min/min_ewm.png',width: 60.w,),
                                    ),
                                  )
                                ],
                              ),
                            ),
                            Container(
                              height: 0.5,
                              margin: EdgeInsets.only(top: 40.w),
                              color: Color(0xffE9E8E9),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 20.w,bottom: 20.w),
                              alignment: Alignment.centerLeft,
                              child: Text('${userInfo.profiles!=null? userInfo.profiles!.trim().length==0?'个人介绍...':userInfo.profiles:'个人介绍...'}',
                                style: TextStyle(
                                  color: Color(0xff616161),
                                  fontSize: 30.sp,
                                  fontWeight: FontWeight.w500,
                                ),
                                maxLines: 2,
                              ),
                            ),
                            Container(
                              child: Row(
                                children: [
                                  Container(
                                    decoration: BoxDecoration(
                                        color: Color(0xffE4E4E4),
                                        borderRadius: BorderRadius.circular(8.w)
                                    ),
                                    padding: EdgeInsets.symmetric(horizontal: 10.w,vertical: 10.w),
                                    child: Row(
                                      children: [
                                        Image.asset(userInfo.sex!=1?'assets/min/min_boy.png':'assets/min/min_gir.png',width: 20.w,),
                                        SizedBox(width: 10.w,),
                                        Text(userInfo.addressRegin??'',style: TextStyle(
                                            color: Color(0xff616161),
                                            fontSize: 20.sp,
                                            fontWeight: FontWeight.w500
                                        ),),
                                      ],
                                    ),
                                  ),
                                  Expanded(child: Container())
                                ],
                              ),
                            ),
                            SizedBox(height: 40.w,),
                            Container(
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceAround,
                                children: [
                                  Visibility(child: GestureDetector(
                                    onTap: (){
                                      Navigator.of(context).pushNamed('MinEditInfoPage').then((value) {
                                        refreshUserInfo(refreshBlock: (){
                                          setState(() {
                                          });
                                        });
                                      });
                                    },
                                    child: Container(
                                      alignment: Alignment.center,
                                      child: Image.asset('assets/min/min_bjzl.png',width: 280.w,),
                                    ),
                                  ),visible:true,),
                                  Visibility(
                                    child: GestureDetector(
                                      onTap: (){
                                        Navigator.of(context).pushNamed('HomeRenWuPage');
                                      },
                                      child: Container(
                                        alignment: Alignment.center,
                                        child: Image.asset('assets/min/min_yqzq.png',width: 280.w,),
                                      ),
                                    ),
                                    visible: true,
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
            SliverAppBar(
              backgroundColor: bgColor,
              pinned:true,
              floating: true,
              primary: false,
              leading: Container(),
              toolbarHeight: tabHeight,
              collapsedHeight: tabHeight,
              bottom: PreferredSize(
                preferredSize: Size(1.sw,80.w),
                child: Column(
                  children: [
                    Container(
                      height: 80.w,
                      color: Colors.white,
                      child: TabBar(
                        controller: _tabController,
                        indicatorColor: Color(0xffEA6B2B),
                        unselectedLabelColor: Color(0xff1A051D),
                        unselectedLabelStyle: TextStyle(
                          fontSize: 30.sp,
                          color: Color(0xff1A051D),
                        ),
                        labelColor: Color(0xffEA6B2B),
                        labelStyle: TextStyle(
                          fontSize: 30.sp,
                          color: Color(0xffEA6B2B),
                          fontWeight: FontWeight.w500,
                        ),
                        tabs: const [
                          Text("作品"),
                          Text("现货"),
                          Text("意向"),
                          Text('收藏')
                        ],
                      ),
                    )
                  ],
                ),
              ),
            ),
          ];
        },
        body: Container(
          child: TabBarView(
            controller: _tabController,
            children:  [
              MinVideoPage(sTyep: 1,isMine:true,uid: userInfo.id??0,),
              MinXianHuoPage(isMine:true,uid: userInfo.id??0,),
              MinVideoPage(sTyep: 2,isMine:true,uid: userInfo.id??0,),
              MinCollectionPage(isMine:true,uid: userInfo.id??0,),
            ],
          ),
        ),
      ),
    );
  }
}
